package cn.sumpu.app.guoguojie.biz.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import cn.sumpu.app.guoguojie.biz.FriendBiz;
import cn.sumpu.app.guoguojie.dao.FriendDao;
import cn.sumpu.app.guoguojie.domain.Ext.MaybeFriend;
import cn.sumpu.app.guoguojie.exception.AppBusinessException;
import cn.sumpu.app.guoguojie.util.Common;
import cn.sumpu.app.guoguojie.util.DateUtil;

public class FriendBizImpl implements FriendBiz {

	private FriendDao friendDao;
	
	public void setFriendDao(FriendDao friendDao) {
		this.friendDao = friendDao;
	}

	//根据门牌号的集合查询出家庭的信息和家里成员的信息
	@Override
	public List<MaybeFriend> getSameAllFamilyInfo(String doorplate)
			throws AppBusinessException {
		return friendDao.getSameAllFamilyInfo(doorplate);  
	}

	//设置推荐的理由
	@Override
	public void setSameFamilyReason(List<MaybeFriend> maybeFriend, String memberId) throws AppBusinessException {
		if(maybeFriend!=null && maybeFriend.size()>0){
			List<MaybeFriend> list;
			List<MaybeFriend> listAgeAddress;
			Map<String,Object> map = new HashMap<String,Object>();  
			map.put("memberId", memberId);
			for(MaybeFriend friend:maybeFriend){
				map.put("doorplate", friend.getDoorplate());
				list = friendDao.getSameBirthdayMemberList(map);
				listAgeAddress = friendDao.setSameTownReason(map);
				//同城且宝宝同龄
				if(list!=null && list.size()>0 && listAgeAddress!=null && listAgeAddress.size()>0){
					friend.setReason("TA家和您家都在"+listAgeAddress.get(0).getReason()+",宝宝"+DateUtil.getAge(list.get(0).getReason()));
				}else{
					listAgeAddress = friendDao.setSameCountyReason(map);
					if(list!=null && list.size()>0 && listAgeAddress!=null && listAgeAddress.size()>0){
						friend.setReason("TA家和您家都在"+listAgeAddress.get(0).getReason()+",宝宝"+DateUtil.getAge(list.get(0).getReason()));
					}else{
						listAgeAddress = friendDao.setSameCityReason(map);
						if(list!=null && list.size()>0 && listAgeAddress!=null && listAgeAddress.size()>0){
							friend.setReason("TA家和您家都在"+listAgeAddress.get(0).getReason()+",宝宝"+DateUtil.getAge(list.get(0).getReason()));
						}else{
							listAgeAddress = friendDao.setSameProvinceReason(map);
							if(list!=null && list.size()>0 && listAgeAddress!=null && listAgeAddress.size()>0){
								friend.setReason("TA家和您家都在"+listAgeAddress.get(0).getReason()+",宝宝"+DateUtil.getAge(list.get(0).getReason()));
							}else{
								//没有同城且宝宝同龄的就再找宝宝同龄的
								if(list!=null && list.size()>0){
									friend.setReason("宝宝"+DateUtil.getAge(list.get(0).getReason()));
								}else{
									list = friendDao.setSameTownReason(map);
									//同镇的
									if(list!=null && list.size()>0){
										friend.setReason("TA家和您家都在"+list.get(0).getReason());
									}else{
										list = friendDao.setSameCountyReason(map);
										//同县的
										if(list!=null && list.size()>0){
											friend.setReason("TA家和您家都在"+list.get(0).getReason());
										}else{
											list = friendDao.setSameCityReason(map);
											//同市的
											if(list!=null && list.size()>0){
												friend.setReason("TA家和您家都在"+list.get(0).getReason());
											}else{
												//同省的
												list = friendDao.setSameProvinceReason(map);
												if(list!=null && list.size()>0){
													friend.setReason("TA家和您家都在"+list.get(0).getReason());
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}
		}
	}

	//你可能感兴趣的街坊
	@Override
	public List<MaybeFriend> nextMayFriendList(String memberId,String change) throws AppBusinessException {
		int pageSize = 4;
		int count=0;
		String type = "city";//默认根据市才查询 
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("memberId", memberId); 
		count=friendDao.getSameAllDoorplateCityCount(map);  
		if(count==0 || count<=4){
			type = "province";//根据市查询少于4条记录就根据省来查
			count=friendDao.getSameAllDoorplateProvinceCount(map);
		}
		MaybeFriend.pageCount =(count+pageSize-1)/pageSize; // 总页数
		if(Common.empty(change)){
			int randomPage=new Random().nextInt(MaybeFriend.pageCount);
			MaybeFriend.page= randomPage+1;
		}else{
			MaybeFriend.page++;
		}
		//如果已经到最后一页了
		if(MaybeFriend.page>MaybeFriend.pageCount){
			MaybeFriend.page=1;  
		}
		map.put("pageSize", pageSize);
		map.put("startId", (MaybeFriend.page-1)*pageSize);
		List<String> list = null;
		if("province".equals(type)){
			list=friendDao.getSameAllDoorplateProvince(map);
		}else{
			list = friendDao.getSameAllDoorplateCity(map);
		}
		String doorplate = Common.parseListToString(list);
		List<MaybeFriend> maybe = getSameAllFamilyInfo(doorplate);
		setSameFamilyReason(maybe, memberId);
		return maybe;
	}
	
}
